package fragments;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;

import beans.RegisterInfoBean;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.MediaPlayerUtils;


/**
 * A simple {@link Fragment} subclass.
 */
public class UserRegisterFragment extends BaseFragment {
    private RegisterInfoBean bean;
    Unbinder unbinder;
    private MyHandler allHandler;

    private int message;//以此消息判断当前在哪个页面
    private FactoryFingerprintRegisterFragment factoryFingerprintRegisterFragment;
    private SupplierFingerprintRegisterFragment supplierFingerprintRegisterFragment;
    private HospitalFingerprintRegisterFragment hospitalFingerprintRegisterFragment;
    private MediaPlayer mediaPlayer;
    private boolean isResume;

    public UserRegisterFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user_register, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        message = 0;
        allHandler = SingletonClass.getSingleton(MyHandler.class);
        initFragment();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initFragment() {
        RegisterRoleFragment registerRoleFragment = new RegisterRoleFragment();
        FragmentTransaction tran = mContext.getSupportFragmentManager().beginTransaction();
        tran.replace(R.id.fl_user_register, registerRoleFragment);
        tran.commit();
    }

    private void replaceFragment(Fragment fragment, String key) {
        FragmentTransaction transaction = mContext.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_user_register, fragment);
        transaction.commit();
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, bean);
        fragment.setArguments(bundle);
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {
            case HandlerConstant.REGISTER_ROLE:
                message = msg.what;
                bean = (RegisterInfoBean) msg.obj;
                if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                    SupplierRegisterFragment supplierRegisterFragment = new SupplierRegisterFragment();
                    replaceFragment(supplierRegisterFragment, Constants.SUPPLIER_REGISTER_FRAGMENT);
                } else if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                    FactoryRegisterFragment factoryRegisterFragment = new FactoryRegisterFragment();
                    replaceFragment(factoryRegisterFragment, Constants.FACTORY_REGISTER_FRAGMENT);
                } else {
                    HospitalRegisterFragment hospitalRegisterFragment = new HospitalRegisterFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("type", bean.getRegisterRole());
                    hospitalRegisterFragment.setArguments(bundle);
                    replaceFragment(hospitalRegisterFragment, Constants.HOSPITAL_REGISTER_FRAGMENT);
                }
                break;
            case HandlerConstant.REGISTER_TYPE:
                message = msg.what;
                bean = (RegisterInfoBean) msg.obj;
                RegisterTypeFragment registerTypeFragment = new RegisterTypeFragment();
                replaceFragment(registerTypeFragment, Constants.REGISTER_TYPE_FRAGMENT);
                break;
            case HandlerConstant.IRIS_REGISTER:
                message = msg.what;
                bean = (RegisterInfoBean) msg.obj;
                if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                    FactoryIrisRegisterFragment factoryIrisRegisterFragment = new FactoryIrisRegisterFragment();
                    replaceFragment(factoryIrisRegisterFragment, Constants.BASE_IRIS_REGISTER_FRAGMENT);
                } else if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                    SupplierIrisRegisterFragment supplierIrisRegisterFragment = new SupplierIrisRegisterFragment();
                    replaceFragment(supplierIrisRegisterFragment, Constants.BASE_IRIS_REGISTER_FRAGMENT);
                } else {
                    HospitalIrisRegisterFragment hospitalIrisRegisterFragment = new HospitalIrisRegisterFragment();
                    replaceFragment(hospitalIrisRegisterFragment, Constants.BASE_IRIS_REGISTER_FRAGMENT);
                }
                break;
            case HandlerConstant.FINGERPRINT_REGISTER:
                message = msg.what;
                bean = (RegisterInfoBean) msg.obj;
                if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                    factoryFingerprintRegisterFragment = new FactoryFingerprintRegisterFragment();
                    replaceFragment(factoryFingerprintRegisterFragment, Constants.BASE_FINGERPRINT_REGISTER_FRAGMENT);
                } else if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                    supplierFingerprintRegisterFragment = new SupplierFingerprintRegisterFragment();
                    replaceFragment(supplierFingerprintRegisterFragment, Constants.BASE_FINGERPRINT_REGISTER_FRAGMENT);
                } else {
                    hospitalFingerprintRegisterFragment = new HospitalFingerprintRegisterFragment();
                    replaceFragment(hospitalFingerprintRegisterFragment, Constants.BASE_FINGERPRINT_REGISTER_FRAGMENT);
                }
                break;

            case HandlerConstant.REGISTER_SUCCESS:
                allHandler.sendEmptyMessage(HandlerConstant.REFRESH_NET); // 管理页面刷新数据
                bean = (RegisterInfoBean) msg.obj;
                message = msg.what;
//                mediaPlayer.release();
//                mediaPlayer = MediaPlayer.create(mContext, R.raw.register_success);
//                mediaPlayer.start();
                RegisterSuccessFragment registerSuccessFragment = new RegisterSuccessFragment();
//                FragmentTransaction transaction1 = mContext.getSupportFragmentManager().beginTransaction();
//                transaction1.replace(R.id.fl_user_register, registerSuccessFragment);
//                transaction1.commit();
                replaceFragment(registerSuccessFragment, Constants.BASE_IRIS_REGISTER_FRAGMENT);
                break;

            case HandlerConstant.REGISTER_FAILURE:
                bean = (RegisterInfoBean) msg.obj;
                RegisterFailureFragment registerFailureFragment = new RegisterFailureFragment();
                replaceFragment(registerFailureFragment, Constants.BASE_IRIS_REGISTER_FRAGMENT);
                break;

            case HandlerConstant.REGISTER_SUCCESS_FRAGMENT_SURE:
                message = msg.what;
                RegisterRoleFragment registerRoleFragment = new RegisterRoleFragment();
                FragmentTransaction transaction = mContext.getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fl_user_register, registerRoleFragment);
                transaction.commit();
                break;

            case HandlerConstant.REGISTER_FAILURE_FRAGMENT_SURE:
                bean = (RegisterInfoBean) msg.obj;
                RegisterTypeFragment registerTypeFragment1 = new RegisterTypeFragment();
                replaceFragment(registerTypeFragment1, Constants.REGISTER_TYPE_FRAGMENT);
                break;

            // 指纹
            case HandlerConstant.FINGERPRINT_REGISTER_FIRST_RETURN:
                if (isResume) {
                    Log.e("GGG", "Register: FINGERPRINT_REGISTER_FIRST_RETURN");
                    if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                        factoryFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw1st);
                    } else if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                        supplierFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw1st);
                    } else {
                        hospitalFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw1st);
                    }
                    MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_up);
                }
                break;

            case HandlerConstant.FINGERPRINT_REGISTER_SECOND:
                Log.e("GGG", "Register: FINGERPRINT_REGISTER_SECOND");
                if (isResume) {
                    MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_down);
                }
                break;

            case HandlerConstant.FINGERPRINT_REGISTER_SECOND_RETURN:
                if (isResume) {
                    if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                        factoryFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw2ed);
                    } else if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                        supplierFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw2ed);
                    } else {
                        hospitalFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw2ed);
                    }
                    MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_up);
                }
                break;

            case HandlerConstant.FINGERPRINT_REGISTER_THIRD:
                if (isResume) {
                    MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_down);
                }
                break;

            case HandlerConstant.FINGERPRINT_REGISTER_THIRD_RETURN:
                if (isResume) {
                    if (bean.getRegisterRole() == Constants.ROLE_FACTORY) {
                        factoryFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw3rd);
                    } else if (bean.getRegisterRole() == Constants.ROLE_SUPPLIER) {
                        supplierFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw3rd);
                    } else {
                        hospitalFingerprintRegisterFragment.getFingerIcon().setImageResource(R.mipmap.zw3rd);
                    }
                }
                break;
        }
    }


    @Override
    public void onUnVisiable() {
        super.onUnVisiable();
        isResume = false;
        if (message == HandlerConstant.FINGERPRINT_REGISTER) {
            Log.e(TAG, "onUnVisiable: ");
            allHandler.sendEmptyMessage(HandlerConstant.CANCLE_FINGERPRINT_COMMAND);//取消指纹的命令
        }
        Log.e("GGG", "Register onUnVisiable: ");
    }

    @Override
    public void onVisiable() {
        super.onVisiable();
        isResume = true;
        Log.e("GGG", "Register onVisiable: ");
        if (message == HandlerConstant.FINGERPRINT_REGISTER) {
            Log.e(TAG, "onVisiable: ");
            allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_FIRST);//发送注册指纹的命令
            MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_down);
        }
    }


}
